home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 16 windows forms / subclassingdemo / subclassingform.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  4.6 KB  |  134 lines

  1. Public Class SubclassingForm
  2.     Inherits System.Windows.Forms.Form
  3.  
  4. #Region " Windows Form Designer generated code "
  5.  
  6.     Public Sub New()
  7.         MyBase.New()
  8.  
  9.         'This call is required by the Windows Form Designer.
  10.         InitializeComponent()
  11.  
  12.         'Add any initialization after the InitializeComponent() call
  13.  
  14.     End Sub
  15.  
  16.     'Form overrides dispose to clean up the component list.
  17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  18.         If disposing Then
  19.             If Not (components Is Nothing) Then
  20.                 components.Dispose()
  21.             End If
  22.         End If
  23.         MyBase.Dispose(disposing)
  24.     End Sub
  25.         Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
  26.     Friend WithEvents lblStatus As System.Windows.Forms.Label
  27.  
  28.     'Required by the Windows Form Designer
  29.     Private components As System.ComponentModel.Container
  30.  
  31.     'NOTE: The following procedure is required by the Windows Form Designer
  32.     'It can be modified using the Windows Form Designer.  
  33.     'Do not modify it using the code editor.
  34.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  35.         Me.CheckBox1 = New System.Windows.Forms.CheckBox()
  36.         Me.lblStatus = New System.Windows.Forms.Label()
  37.         Me.SuspendLayout()
  38.         '
  39.         'CheckBox1
  40.         '
  41.         Me.CheckBox1.Location = New System.Drawing.Point(24, 24)
  42.         Me.CheckBox1.Name = "CheckBox1"
  43.         Me.CheckBox1.Size = New System.Drawing.Size(168, 24)
  44.         Me.CheckBox1.TabIndex = 1
  45.         Me.CheckBox1.Text = "Enable subclassing"
  46.         '
  47.         'lblStatus
  48.         '
  49.         Me.lblStatus.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
  50.         Me.lblStatus.Dock = System.Windows.Forms.DockStyle.Bottom
  51.         Me.lblStatus.Location = New System.Drawing.Point(0, 141)
  52.         Me.lblStatus.Name = "lblStatus"
  53.         Me.lblStatus.Size = New System.Drawing.Size(456, 32)
  54.         Me.lblStatus.TabIndex = 0
  55.         '
  56.         'SubclassingForm
  57.         '
  58.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  59.         Me.ClientSize = New System.Drawing.Size(456, 173)
  60.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.CheckBox1, Me.lblStatus})
  61.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  62.         Me.Name = "SubclassingForm"
  63.         Me.Text = "SubclassingForm"
  64.         Me.ResumeLayout(False)
  65.  
  66.     End Sub
  67.  
  68. #End Region
  69.  
  70.     ' API constants
  71.     Private Const WM_NCHITTEST As Integer = &H84
  72.     Private Const WM_ACTIVATEAPP As Integer = &H1C
  73.     Private Const WM_DISPLAYCHANGE As Integer = &H7E
  74.     Private Const WM_COMPACTING As Integer = &H41
  75.     Private Const WM_GETMINMAXINFO As Integer = &H24
  76.     Private Const HTCAPTION As Integer = 2
  77.     Private Const HTCLIENT As Integer = 1
  78.  
  79.     ' API structures
  80.     Structure POINTAPI
  81.         Dim X As Integer
  82.         Dim Y As Integer
  83.     End Structure
  84.  
  85.     Structure MINMAXINFO
  86.         Dim ptReserved As POINTAPI
  87.         Dim ptMaxSize As POINTAPI
  88.         Dim ptMaxPosition As POINTAPI
  89.         Dim ptMinTrackSize As POINTAPI
  90.         Dim ptMaxTrackSize As POINTAPI
  91.     End Structure
  92.  
  93.     ' this is where we do the subclassing
  94.  
  95.     Protected Overrides Sub WndProc(ByRef m As Message)
  96.         ' let the base form process this message
  97.         MyBase.WndProc(m)
  98.  
  99.         ' exit if subclassing is disabled
  100.         If Not CheckBox1.Checked Then Exit Sub
  101.  
  102.         Select Case m.Msg
  103.             Case WM_DISPLAYCHANGE
  104.                 lblStatus.Text = "Screen resolution has changed"
  105.  
  106.             Case WM_COMPACTING
  107.                 lblStatus.Text = "The system is low on memory"
  108.  
  109.             Case WM_ACTIVATEAPP
  110.                 ' application has been activated or deactivated
  111.                 If m.WParam.ToInt32 <> 0 Then
  112.                     lblStatus.Text = "Application has been activated"
  113.                 Else
  114.                     lblStatus.Text = "Application has been deactivated"
  115.                 End If
  116.  
  117.             Case WM_NCHITTEST
  118.                 ' if on client area, make Windows believe it's on caption
  119.                 If m.Result.ToInt32 = HTCLIENT Then
  120.                     ' the only way to assing an IntPtr
  121.                     m.Result = New IntPtr(HTCAPTION)
  122.                 End If
  123.  
  124.             Case WM_GETMINMAXINFO
  125.                 Dim mmi As MINMAXINFO
  126.                 mmi = CType(m.GetLParam(mmi.GetType), MINMAXINFO)
  127.                 lblStatus.Text = String.Format("Max size = ({0}, {1})", mmi.ptMaxSize.X, mmi.ptMaxSize.Y)
  128.         End Select
  129.  
  130.     End Sub
  131.  
  132.  
  133. End Class
  134.